package com.android.citynetworkpin;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import com.android.citynetworkpin.cache.DiskLruCacheHelper;
import com.android.citynetworkpin.model.group.MainSearchSortData;
import com.android.citynetworkpin.utils.ConstantsUrl;
import com.android.citynetworkpin.utils.MyLog;
import com.android.citynetworkpin.model.group.MainSearchSortDataCallback;
import com.android.citynetworkpin.utils.HttpUtils;
import com.android.citynetworkpin.utils.ObjectUtils;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

public class WelcomeActivity extends AppCompatActivity {
    public static WelcomeActivity intance = null;
    private WelcomeActivity mcontext;
    private DiskLruCacheHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        intance = this;
        mcontext = WelcomeActivity.this;
        ObjectUtils.StatusBarWhiteBg(mcontext, false);
        try {
            helper = new DiskLruCacheHelper(mcontext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        GetSortData();
        initData();
    }

    private void initData() {

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(mcontext, MainActivity.class));
                finish();

            }
        }, 1000);
    }

    private void GetSortData() {
        boolean networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.SORTLISTDATA;
            MyLog.e("地址", address);
            HttpUtils.get(address, null, new MainSearchSortDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(MainSearchSortData response, int id) {
                    if (response == null) {
                        return;
                    }
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH, response.toString());
                    }
                }
            });
        } else {

        }
    }
}
